package com.xcity.game.combat.skill.impl;

import java.util.List;

import com.xcity.game.combat.Action;
import com.xcity.game.combat.ActionEffect;
import com.xcity.game.combat.CombatUtils;
import com.xcity.game.combat.Round;
import com.xcity.game.combat.attr.CombatAttributeType;
import com.xcity.game.combat.buff.CombatBuff;
import com.xcity.game.combat.skill.CombatSkill;
import com.xcity.game.combat.unit.CombatUnit;
import com.xcity.game.common.GameStaticConfig;
import com.xcity.game.skill.Skill;

/**
 * 大刀阔斧<br/>
 * 一次群体攻击，造成{0}技能伤害，若此次攻击有敌方阵亡，每阵亡一个敌方目标下一次普通攻击伤害增加{1}%
 * @author yang.li
 *
 */
public class COMBAT_SKILL_90130 extends CombatSkill {

	public COMBAT_SKILL_90130(Skill skill) {
		super(skill);
	}

	@Override
	public void calcDamage(Action action) {
		CombatUnit source = action.unit;
		List<CombatUnit> targets = action.target.all();
		float rate = skill.getParameter(0) / GameStaticConfig.percentBase;
		for (int i = 0; i < targets.size(); i++) {
			CombatUnit target = targets.get(i);
			if (!target.isDied()) {
				ActionEffect eff = action.addTarget(target);
				CombatUtils.calcMagicAttack(eff, rate);
				if (eff.getUnit().isDied()) {
					action.addBuff(new BUFF(skill, source));
				}
			}
		}
	}
	
	static class BUFF extends CombatBuff {
		int oldAttackPower, newAttackPower;
		public BUFF(Skill skill, CombatUnit owner) {
			super(null, owner, 1);
			active = true;
			int power = (int) owner.getAttackPower();
			oldAttackPower = power;
			power += power * skill.getParameter(1) / GameStaticConfig.percentBase;
			newAttackPower = power;
		}
		@Override
		public int getId() {
			return 90130;
		}

		@Override
		public void preAction(Action action) {
			active = true;
			owner.setAttributeValue(CombatAttributeType.ATTACK_POWER, newAttackPower);
		}

		@Override
		public void afterAction(Action action) {
			uneffect();
			action.ref().addBuff(this);
		}

		@Override
		public void roundEnd(Round round) {
			super.roundEnd(round);
			if (state == STATE_REMOVED) {
				owner.setAttributeValue(CombatAttributeType.ATTACK_POWER, oldAttackPower);
			}
		}
	}

}
